package cn.baddonkey.photography.agent.controller;

import cn.baddonkey.photography.agent.config.security.TokenService;
import cn.baddonkey.photography.agent.core.APIResponseStatus;
import cn.baddonkey.photography.agent.core.PageData;
import cn.baddonkey.photography.agent.core.PlatformResponse;
import cn.baddonkey.photography.agent.entity.dto.LoginBody;
import cn.baddonkey.photography.agent.entity.dto.LoginUser;
import cn.baddonkey.photography.agent.entity.dto.UserInfoPageDto;
import cn.baddonkey.photography.agent.entity.dto.UserUpdateDto;
import cn.baddonkey.photography.agent.service.UserInfoService;
import cn.baddonkey.photography.agent.utils.SecurityUtils;
import cn.baddonkey.photography.agent.entity.vo.UserInfoVo;

import io.swagger.v3.oas.annotations.Operation;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/** 用户模块 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private TokenService tokenService;

    /**
     * 获取当前登录用户信息
     *
     * @return 结果
     */
    @PostMapping("/info")
    @Operation(summary = "获取当前登录用户信息", description = "获取当前登录用户信息")
    public PlatformResponse<UserInfoVo> info() {
        // 生成令牌
        UserInfoVo info = userInfoService.info();
        return PlatformResponse.ok(info);
    }

    /**
     * 用户登录方法
     *
     * @param loginBody
     *            登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @Operation(summary = "登录", description = "登录")
    public PlatformResponse<String> login(@RequestBody @Validated LoginBody loginBody) {
        // 生成令牌
        LoginUser loginUser = userInfoService.login(loginBody);
        String token = tokenService.createToken(loginUser);
        return PlatformResponse.ok(token);
    }

    /**
     * 获取用户列表
     *
     * @param userPageDto
     *            用户分页查询条件
     * @return 用户分页数据
     */
    @PostMapping("/management/list")
    @Operation(summary = "用户列表", description = "用户列表", method = "POST")
    public PlatformResponse<PageData<UserInfoVo>> list(@RequestBody UserInfoPageDto userPageDto) {
        PageData<UserInfoVo> pageData = userInfoService.list(userPageDto);
        return PlatformResponse.ok().resultData(pageData);
    }

    /**
     * 根据用户ID删除用户
     *
     * @param userIds
     *            用户ID数组
     * @return 删除结果
     */
    @GetMapping("/management/{userIds}")
    @Operation(summary = "删除用户", description = "删除用户", method = "DELETE")
    public PlatformResponse remove(@PathVariable String[] userIds) {
        String currentUserId = SecurityUtils.getUserId();
        if (ArrayUtils.contains(userIds, currentUserId)) {
            return PlatformResponse.error(APIResponseStatus.U9999, "当前用户不能删除");
        }
        userInfoService.deleteUserByIds(userIds);
        return PlatformResponse.ok();
    }

    /**
     * 重置用户密码
     *
     * @param user
     *            用户信息
     * @return 重置结果
     */
    @PostMapping("/management/resetPwd")
    @Operation(summary = "重置密码", description = "重置密码", method = "POST")
    public PlatformResponse resetPwd(@RequestBody @Validated UserUpdateDto user) {
        userInfoService.resetPwd(user);
        return PlatformResponse.ok();
    }

    /**
     * 修改用户状态
     *
     * @param user
     *            用户信息
     * @return 修改结果
     */
    @PostMapping("/management/changeStatus")
    @Operation(summary = "修改状态", description = "修改状态", method = "POST")
    public PlatformResponse changeStatus(@RequestBody @Validated UserUpdateDto user) {
        userInfoService.updateUserStatus(user);
        return PlatformResponse.ok();
    }

    /**
     * 修改用户信息
     *
     * @param user
     *            用户信息
     * @return 修改结果
     */
    @PostMapping("/management/update")
    @Operation(summary = "修改用户", description = "修改用户", method = "POST")
    public PlatformResponse edit(@Validated @RequestBody UserUpdateDto user) {
        if (StringUtils.isNotEmpty(user.getPhone()) && !userInfoService.checkPhoneUnique(user)) {
            return PlatformResponse.error(
                    APIResponseStatus.U9999, "修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail())
                && !userInfoService.checkEmailUnique(user)) {
            return PlatformResponse.error(
                    APIResponseStatus.U9999, "修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        userInfoService.updateUser(user);
        return PlatformResponse.ok();
    }

    /**
     * 新增用户
     *
     * @param user
     *            用户信息
     * @return 结果
     */
    @PostMapping("/management/insert")
    @Operation(summary = "新增用户", description = "新增用户", method = "POST")
    public PlatformResponse add(@Validated @RequestBody UserUpdateDto user) {
        if (!userInfoService.checkUserNameUnique(user)) {
            return PlatformResponse.error(
                    APIResponseStatus.U9999, "新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhone())
                && !userInfoService.checkPhoneUnique(user)) {
            return PlatformResponse.error(
                    APIResponseStatus.U9999, "新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail())
                && !userInfoService.checkEmailUnique(user)) {
            return PlatformResponse.error(
                    APIResponseStatus.U9999, "新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        userInfoService.insertUser(user);
        return PlatformResponse.ok();
    }
}
